package com.eeepay.eeepay_v2.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import com.bigkoo.pickerview.TimePickerView;
import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.model.CustomTime;
import com.eeepay.v2_library.log.LogUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by zw on 2016/3/22 0022.
 * 时间选择工具类
 */
public class TimeUtils {

    public static final int DATE = 0;
    public static final int TIME = 1;
    public static final int DATE_TIME = 2;

    public static void selectTime(final Context mContext, final TextView tv, final int mode) {
        final CustomTime time = new CustomTime();
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.d("time = " + time.toString());
                View view = View.inflate(mContext, R.layout.time_select_layout, null);
                final DatePicker datePicker = (DatePicker) view.findViewById(R.id.new_act_date_picker);
                final TimePicker timePicker = (TimePicker) view.findViewById(R.id.new_act_time_picker);
                datePicker.setSpinnersShown(true);
                datePicker.setCalendarViewShown(false);

                // Init DatePicker
                int year;
                int month;
                int day;
                if (TextUtils.isEmpty(tv.getText().toString())) {
                    // Use the current date as the default date in the picker
                    final Calendar c = Calendar.getInstance();
                    year = c.get(Calendar.YEAR);
                    month = c.get(Calendar.MONTH);
                    day = c.get(Calendar.DAY_OF_MONTH);
                } else {
                    year = time.getYear();
                    month = time.getMonth() - 1;
                    day = time.getDay();
                }
                datePicker.init(year, month, day, null);

                if (mode == DATE_TIME) {//日期时间
                    // Init TimePicker
                    timePicker.setVisibility(View.VISIBLE);
                    int hour;
                    int minute;
                    if (TextUtils.isEmpty(tv.getText().toString())) {
                        // Use the current time as the default values for the picker
                        final Calendar c = Calendar.getInstance();
                        hour = c.get(Calendar.HOUR_OF_DAY);
                        minute = c.get(Calendar.MINUTE);
                    } else {
                        hour = time.getHour();
                        minute = time.getMinute();
                    }
                    timePicker.setIs24HourView(true);
                    timePicker.setCurrentHour(hour);
                    timePicker.setCurrentMinute(minute);
                } else {
                    timePicker.setVisibility(View.GONE);
                }

                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setView(view);
                builder.setTitle("选择时间");
                builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String timeStr;
                        time.setYear(datePicker.getYear());
                        time.setMonth(datePicker.getMonth() + 1);
                        time.setDay(datePicker.getDayOfMonth());
                        if (mode == DATE_TIME) {
                            time.setHour(timePicker.getCurrentHour());
                            time.setMinute(timePicker.getCurrentMinute());
                            timeStr = time.time2String();
                        } else {
                            timeStr = time.date2String();
                        }
                        tv.setText(timeStr);
                    }
                });
                builder.show();
            }
        });
    }


    /**
     * 获取当前日期
     *
     * @return
     */
    public static String getNowDate() {
        int year;
        int month;
        int day;
        Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
        return year + "-" + month + "-" + day;
    }

    /**
     * 获取当前时间  具体到秒
     *
     * @return
     */
    public static String getNowTime() {
        int year;
        int month;
        int day;
        Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
        return year + "-" + month + "-" + day + " " + c.get(Calendar.HOUR) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND);
    }

    /**
     * 截取日期
     *
     * @param time 2016-04-26 00:00:00 格式的时间
     * @return 返回2016-04-26格式
     */
    public static String getDate(String time) {
        int index = time.length();
        if (time.contains(" ")) {
            index = time.indexOf(" ");
        }
        return time.substring(0, index);
    }

    /**
     * 截取日期  不带年份  只要月-日
     *
     * @param time 2016-04-26 00:00:00 格式的时间
     * @return 返回04-26格式
     */
    public static String getDateNoYear(String time) {
        int startIndex = time.indexOf("-");
        int endIndex = time.length();
        if (time.contains(" ")) {
            endIndex = time.indexOf(" ");
        }
        return time.substring(startIndex + 1, endIndex);
    }

    //原先的时间日期选择控件太烂，加一个新的
    public static void newDateTimePicker(Context mContext, final TextView tv, int mode){
        //时间选择器
        final TimePickerView pvTime;
        final String formatStr;
        if (mode == DATE_TIME) {//日期时间
            pvTime = new TimePickerView(mContext, TimePickerView.Type.ALL);
            formatStr = "yyyy-MM-dd HH:mm";
        }else {
            pvTime = new TimePickerView(mContext, TimePickerView.Type.YEAR_MONTH_DAY);
            formatStr = "yyyy-MM-dd";
        }
        String time = tv.getText().toString();
        if(!TextUtils.isEmpty(time)){
            pvTime.setTime(new Date(time));
        }else {
            pvTime.setTime(new Date());
        }
        pvTime.setTitle("选择时间");
        pvTime.setCyclic(false);
        pvTime.setCancelable(true);
        //时间选择后回调
        pvTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                SimpleDateFormat format = new SimpleDateFormat(formatStr);
                String str = format.format(date);
                tv.setText(str);
            }
        });

        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pvTime.show();
            }
        });
    }


}
